// 权限检查工具函数
export interface UserInfo {
  id: number;
  username: string;
  role: string;
  disabled: boolean;
}

// 从 localStorage 获取当前用户信息
export function getCurrentUser(): UserInfo | null {
  try {
    const sessionStr = localStorage.getItem('current_session');
    if (!sessionStr) return null;
    
    const session = JSON.parse(sessionStr);
    return {
      id: session.id || 0,
      username: session.username || '',
      role: session.role || 'user',
      disabled: session.disabled || false
    };
  } catch {
    return null;
  }
}

// 检查是否为管理员
export function isAdmin(): boolean {
  const user = getCurrentUser();
  return user?.role === 'admin';
}

// 检查用户是否有权限访问某个功能
export function hasPermission(requiredRole: 'admin' | 'user' = 'user'): boolean {
  const user = getCurrentUser();
  if (!user) return false;
  
  if (requiredRole === 'admin') {
    return user.role === 'admin';
  }
  
  return true; // 普通用户权限
}
